package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.CompanyAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.ToptenantEntity;
import com.runen.infinitemovement.widget.popupwind.InputTenantPassWordPopupWindow;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class InputTeamCodeActivity extends BaseActivity {
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.et_input)
    EditText et_input;
    @BindView(R.id.tv_join)
    TextView tv_join;
    @BindView(R.id.tv_no_code)
    TextView tv_no_code;
    @BindView(R.id.top_iv_right)
    ImageView top_iv_right;

    private int changeBranch;
    public static final int REQUEST_CODE = 0x001;
    private String topId;
    private InputTenantPassWordPopupWindow popupWindow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_team_code;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("输入军团码");
        changeBranch = getIntent().getIntExtra("changeBranch", 0);
        top_iv_right.setImageResource(R.mipmap.ico_scan_code);
        top_iv_right.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.top_btn_left, R.id.tv_no_code, R.id.top_iv_right, R.id.tv_join})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.tv_no_code:
                Intent intent = new Intent(this, CheckJoinUsRecommendActivity.class);
                intent.putExtra("changeBranch", changeBranch);
                startActivity(intent);
                break;
            case R.id.top_iv_right:
                startActivityForResult(new Intent(InputTeamCodeActivity.this, CaptureActivity.class), REQUEST_CODE);
                break;
            case R.id.tv_join:
                if (TextUtils.isEmpty(et_input.getText().toString())) {
                    ToastUtils.showShort("请输入军团码");
                    return;
                }
                getCodeTopId(et_input.getText().toString());
                break;
        }
    }

    private void getCodeTopId(String code) {
        String str = "?tenantCode=%s";
        new CompanyAPI().getTenantCodeTopId(String.format(str, code), new BaseResultCallback<ToptenantEntity>() {
            @Override
            public void onResponse(ToptenantEntity response) {
                if (response != null && response.code == 0) {
                    if (response.data == null) {
                        ToastUtils.showShort("军团码不存在");
                    } else {
                        topId = response.data.topTenantId;
                        if (response.data.passwordStatus) {
                            popupWindow = InputTenantPassWordPopupWindow.getInstance(InputTeamCodeActivity.this,
                                    new InputTenantPassWordPopupWindow.InputContent() {
                                        @Override
                                        public void inputContent(String password) {
                                            verifyPassword(topId, password);
                                        }
                                    });
                            popupWindow.show();
                        } else {
                            jumpToCheckTeamActivity(topId);
                        }
                    }
                } else {
                    ToastUtils.showShort("军团码不存在");
                }
            }
        });
    }

    private void jumpToCheckTeamActivity(String companyId) {
        Intent intent = new Intent(InputTeamCodeActivity.this, CheckTeamActivity.class);
        if (changeBranch == 1) {
            intent.putExtra("type", 1);
        }
        intent.putExtra("companyId", companyId);
        startActivity(intent);
    }

    /**
     * 验证密码是否正确
     *
     * @param companyId
     * @param password
     */
    private void verifyPassword(String companyId, String password) {
        showDialog();
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", companyId);
            object.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new CompanyAPI().postVerifyPassword(object.toString(), new BaseResultCallback<BaseResponseEntity>() {
            @Override
            public void onResponse(BaseResponseEntity response) {
                dismissDialog();
                if (response != null && response.getCode() == 0) {
                    jumpToCheckTeamActivity(companyId);
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE:
                    String result = CameraScan.parseScanResult(data);
                    getCodeTopId(result);
                    break;
            }
        }
    }
}
